package com.lxl.bt.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.picture.lib.tools.ToastUtils;
import com.lxl.bt.Constant;
import com.lxl.bt.R;
import com.lxl.bt.activity.AboutUsActivity;
import com.lxl.bt.activity.BTCActivity;
import com.lxl.bt.activity.BaseFragment;
import com.lxl.bt.activity.MyAgentActivity;
import com.lxl.bt.activity.OrderListActivity;
import com.lxl.bt.activity.SettingActivity;
import com.lxl.bt.activity.ShareActivity;
import com.lxl.bt.activity.WebviewActivity;
import com.lxl.bt.activity.YouHuiQuanListActivity;
import com.lxl.bt.bean.A_Login;
import com.lxl.bt.bean.UserAccountBean;
import com.lxl.bt.net.NetApi;
import com.lxl.bt.net.Result;
import com.lxl.bt.retorfit.NetCallback;
import com.lxl.bt.retorfit.RetrofitUtil;
import com.lxl.bt.util.ACache;
import com.lxl.bt.util.Prefs;
import com.yanzhenjie.sofia.Sofia;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Create by lvxinlong
 * on 我的
 * on 2020年12月5日10:42:43
 **/
public class MyFragment extends BaseFragment {


    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.txt_toolbar_title)
    TextView txtToolbarTitle;
    @BindView(R.id.rlBack)
    LinearLayout rlBack;
    @BindView(R.id.ivSetting)
    ImageView ivSetting;
    @BindView(R.id.rlRight)
    RelativeLayout rlRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.ivShow)
    ImageView ivShow;
    @BindView(R.id.tvZiChan)
    TextView tvZiChan;
    @BindView(R.id.ivDetail)
    ImageView ivDetail;
    @BindView(R.id.ll1_1)
    LinearLayout ll11;
    @BindView(R.id.ll1_2)
    LinearLayout ll12;
    @BindView(R.id.llOrder)
    LinearLayout llOrder;
    @BindView(R.id.llKa)
    LinearLayout llKa;
    @BindView(R.id.llQuestion)
    LinearLayout llQuestion;
    @BindView(R.id.llAbout)
    LinearLayout llAbout;
    @BindView(R.id.cardbtc)
    CardView cardbtc;
    @BindView(R.id.tvUsdt)
    TextView tvUsdt;
    @BindView(R.id.tvBtc)
    TextView tvBtc;
    @BindView(R.id.tvEth)
    TextView tvEth;
    @BindView(R.id.tvLtc)
    TextView tvLtc;
    @BindView(R.id.tvFil)
    TextView tvFil;
    @BindView(R.id.cardusdt)
    CardView cardusdt;
    @BindView(R.id.cardeth)
    CardView cardeth;
    @BindView(R.id.cardltc)
    CardView cardltc;
    @BindView(R.id.cardfil)
    CardView cardfil;
    @BindView(R.id.carddianfei)
    CardView carddianfei;
    @BindView(R.id.ll_my_agent)
    LinearLayout llMyAgent;


    private Unbinder unbinder;
    ACache aCache;
    A_Login a_login;
    UserAccountBean userAccount;
    boolean showValue=true;
    public static MyFragment newInstance() {
        Bundle bundle = new Bundle();
        MyFragment fragment = new MyFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_my, container, false);
//        ((BaseActivity) getActivity()).setStatusBarFullTransparent();
        //返回一个Unbinder值（进行解绑），注意这里的this不能使用getActivity()
        unbinder = ButterKnife.bind(this, view);
//        EventBus.getDefault().register(this);
        Sofia.with(getActivity()).statusBarBackgroundAlpha(0)
                .invasionStatusBar();
//                .statusBarLightFont();
//                .statusBarDarkFont();


        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        aCache = ACache.get(getActivity());
        a_login = (A_Login) aCache.getAsObject(Constant.LOGIN);
        int jobId = Prefs.getIntValue(Constant.JOB_ID);
        if (jobId == 17){
            llMyAgent.setVisibility(View.VISIBLE);
        }else{
            llMyAgent.setVisibility(View.GONE);
        }
    }

    @Override
    public void getData() {
        txtToolbarTitle.setText(Prefs.getStringValue(Constant.PHONE));
        getInfo();
    }

    public void getInfo() {
        RetrofitUtil.getInstance().create(NetApi.class, 0)
                .btUserAccount().enqueue(new NetCallback<Result<UserAccountBean>>() {
            @Override
            public void onSuccess(Result<UserAccountBean> result) {
                if (result.isSuccess()) {
                    userAccount = result.getData();
                    tvZiChan.setText(String.format("≈￥%s", userAccount.getTotal()));
                    tvUsdt.setText(userAccount.getUsdtAmount() + "");
                    tvBtc.setText(userAccount.getBtcAmount() + "");
                    tvEth.setText(userAccount.getEthAmount() + "");
                    tvLtc.setText(userAccount.getLtcAmount() + "");
                    tvFil.setText(userAccount.getFilAmount() + "");
                }
            }

            @Override
            public void netLogin() {

            }

            @Override
            public void onError(String errorStr) {
                super.onError(errorStr);
                Log.w("lxl", errorStr);

            }

        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        aCache = ACache.get(getActivity());
        a_login = (A_Login) aCache.getAsObject(Constant.LOGIN);
    }


    /**
     * onDestroyView中进行解绑操作
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

    }


    @OnClick({R.id.cardusdt, R.id.cardbtc, R.id.cardeth, R.id.cardltc, R.id.cardfil, R.id.carddianfei,
            R.id.rlRight, R.id.ivBack, R.id.rlBack, R.id.ivShow, R.id.ivDetail, R.id.ll1_1, R.id.ll1_2,
            R.id.llOrder, R.id.llKa, R.id.llQuestion, R.id.llAbout,R.id.ll_my_agent})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cardusdt:
                startActivity(new Intent(getActivity(), BTCActivity.class).putExtra("data", userAccount)
                        .putExtra("coinName", "usdt")
                        .putExtra("name", 0));
                break;
            case R.id.cardbtc:
                startActivity(new Intent(getActivity(), BTCActivity.class).putExtra("data", userAccount)
                        .putExtra("coinName", "btc")
                        .putExtra("name", 1));
                break;
            case R.id.cardeth:
                startActivity(new Intent(getActivity(), BTCActivity.class).putExtra("data", userAccount)
                        .putExtra("coinName", "eth")
                        .putExtra("name", 2));
                break;
            case R.id.cardltc:
                startActivity(new Intent(getActivity(), BTCActivity.class).putExtra("data", userAccount)
                        .putExtra("coinName", "ltc")
                        .putExtra("name", 3));
                break;
            case R.id.cardfil:
                startActivity(new Intent(getActivity(), BTCActivity.class).putExtra("data", userAccount)
                        .putExtra("coinName", "fil")
                        .putExtra("name", 4));
                break;
            case R.id.carddianfei:
                startActivity(new Intent(getActivity(), BTCActivity.class).putExtra("data", userAccount)
                        .putExtra("coinName", "light")
                        .putExtra("name", 5));
                break;
            case R.id.rlRight:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            case R.id.ivBack:
                break;
            case R.id.rlBack:
                break;
            case R.id.ivShow:
                showValue=!showValue;
                if(showValue){
                    tvZiChan.setText(String.format("≈￥%s", userAccount.getTotal()));
                    ivShow.setImageResource(R.mipmap.show);
                }else{
                    tvZiChan.setText("****");
                    ivShow.setImageResource(R.mipmap.hide);
                }
                break;
            case R.id.ivDetail:
                break;
            case R.id.ll1_1:
                ToastUtils.s(getActivity(),"暂未开放!");
                break;
            case R.id.ll1_2:
                startActivity(new Intent(getActivity(), ShareActivity.class));
                break;
            case R.id.llOrder:
                startActivity(new Intent(getActivity(), OrderListActivity.class));
                break;
            case R.id.llKa:
                startActivity(new Intent(getActivity(), YouHuiQuanListActivity.class));
                break;
            case R.id.llQuestion:
                WebviewActivity.intent(getActivity(),"常见问题","http://oss.puxkj.com//default/1619350690440.html");
                break;
            case R.id.llAbout:
                startActivity(new Intent(getActivity(), AboutUsActivity.class));
                break;
            case R.id.ll_my_agent:
                MyAgentActivity.intent(getContext());
                break;
        }
    }
}
